package net.jforum.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.UserFriendDAO;
import net.jforum.entities.KarmaStatus;
import net.jforum.entities.Topic;
import net.jforum.entities.User;
import net.jforum.exceptions.DatabaseException;
import net.jforum.util.DbUtils;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @ author  张洋 jackson.zhang@ericsson.com
 * @ version 创建时间 ：Aug 18, 2010  11:40:14 AM
 * @ description : 
 */
public class GenericUserFriendDAO  extends AutoKeys implements UserFriendDAO{
	@Override
	public boolean addOneRelationByIds(int userId, int friendId) {
		PreparedStatement p = null;
		ResultSet rs = null;

		try {
			p = JForumExecutionContext.getConnection().prepareStatement(
					SystemGlobals.getSql("FriendRelationModel.isFriendAdded"));
			p.setInt(1, userId);
			p.setInt(2, friendId);
			rs = p.executeQuery();
			int added = 0;
			if (rs.next()) {
				added = rs.getInt("added");
			}
			if (added == 0) {
				p = JForumExecutionContext.getConnection().prepareStatement(
						SystemGlobals.getSql("FriendRelationModel.add"));
				p.setInt(1, userId);
				p.setInt(2, friendId);
				p.executeUpdate();
				return true;
			} else
				return false;
		} catch (SQLException e) {
			throw new DatabaseException(e);
		} finally {
			DbUtils.close(p);
		}
	}
	@Override
	public void addOneRelation(User user, User friend) {
		
		PreparedStatement p = null;

	try {
		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("FriendRelationModel.add"));
		p.setInt(1, user.getId());
		p.setInt(2, friend.getId());
		p.executeUpdate();
	}
	catch (SQLException e) {
		throw new DatabaseException(e);
	}
	finally {
		DbUtils.close(p);
	}
	}

	@Override
	public void deleteByUserAndFriend(int userId,int friendId) {

		
		PreparedStatement p = null;
	try {
		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("FriendRelationModel.deleteByUserAndFriend"));
		p.setInt(1, userId);
		p.setInt(2, friendId);

		p.executeUpdate();
	}
	catch (SQLException e) {
		throw new DatabaseException(e);
	}
	finally {
		DbUtils.close(p);
	}
	
	}

	@Override
	public boolean isFriendAdded(int userId,int friendId) {
		PreparedStatement p = null;
		ResultSet rs = null;
	try {
		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("FriendRelationModel.isFriendAdded"));
		p.setInt(1, userId);
		p.setInt(2, friendId);
		rs = p.executeQuery();
		int added=0;
		if (rs.next()) {
			added = rs.getInt("added");
		}
		return added>0;
	}
	catch (SQLException e) {
		throw new DatabaseException(e);
	}
	finally {
		DbUtils.close(p);
	}
	
	
	}

	@Override
	public List selectAllFriendsByUserId(int userId) {
		PreparedStatement p = null;
		ResultSet rs = null;
		try {
			p = JForumExecutionContext.getConnection().prepareStatement(
					SystemGlobals.getSql("FriendRelationModel.selectAllFriendsByUser"));
			p.setInt(1, userId);
			rs = p.executeQuery();
			return this.processSelectAll(rs);
		}
		catch (SQLException e) {
			throw new DatabaseException(e);
		}
		finally {
			DbUtils.close(rs, p);
		}
	
	}

	protected List processSelectAll(ResultSet rs) throws SQLException
	{
		List list = new ArrayList();

		while (rs.next()) {
			User u = new User();

			u.setEmail(rs.getString("user_email"));
			u.setId(rs.getInt("user_id"));
			u.setTotalPosts(rs.getInt("user_posts"));
			u.setRegistrationDate(new Date(rs.getTimestamp("user_regdate").getTime()));
			u.setUsername(rs.getString("username"));
			u.setDeleted(rs.getInt("deleted"));
			KarmaStatus karma = new KarmaStatus();
			karma.setKarmaPoints(rs.getInt("user_karma"));
			u.setKarma(karma);
			u.setFrom(rs.getString("user_from"));
			u.setWebSite(rs.getString("user_website"));
			u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1);
			list.add(u);
		}

		return list;
	}

}
